﻿<?php 
class Profile extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('medicines');
		$this->load->model('cal_model');
		$this->load->model('users');
	}
	
	function index(){
		$today = date('Y-m-d');
		$cal_id = $this->cal_model->get_calendar($this->session->userdata('user_id'));
		$data['events'] = $this->cal_model->get_today_event($today, $cal_id);
		$this->load->view('profile/header');
		$this->load->view('profile/welcome',$data);
		$this->load->view('profile/footer');
	}
	
	function profile_view(){
		if($this->session->userdata('care_id')){
			$data['care_info'] = $this->users->get_care($this->session->userdata('care_id'));
		}
		else{
			$care_id = $this->users->get_care_id($this->session->userdata('user_id'));
			$data['care_info'] = $this->users->get_care($care_id);
		}
		$data['user_info'] = $this->users->get_user($this->session->userdata('user_id'));
		$this->load->view('profile/header');
		$this->load->view('profile/profile_view', $data);
		$this->load->view('profile/footer');
	}
	
	function med_report(){
		$query = $this->medicines->get_medicine_history($this->session->userdata('user_id'));
		
		$data['med'] = $query;
		$this->load->view('profile/header');
		$this->load->view('profile/report_view', $data);
		$this->load->view('profile/footer');
		
	}
	function med_taking(){
		$query = $this->medicines->get_user_medicine($this->session->userdata('user_id'));
		
		$data['med'] = $query;
		$this->load->view('profile/header');
		$this->load->view('profile/med_taking_view', $data);
		$this->load->view('profile/footer');
		
		
	}
	
	function edit_page(){
		if($this->session->userdata('care_id')){
			$data['care_info'] = $this->users->get_care($this->session->userdata('care_id'));
		}
		else{
			$care_id = $this->users->get_care_id($this->session->userdata('user_id'));
			$data['care_info'] = $this->users->get_care($care_id);
		}
		$data['user_info'] = $this->users->get_user($this->session->userdata('user_id'));
		$this->load->view('profile/header');
		$this->load->view('profile/edit_view', $data);
		$this->load->view('profile/footer');
	}
	
	function add_med_noti(){
		$med_info = array(
			'routine_id' => $this->get_routine($this->input->post('routine')),
			'type_id' => $this->get_type($this->input->post('type')),
			'med_name' => $this->input->post('name'),
			'dose' => $this->input->post('dose'),
			'unit_id' => $this->get_unit($this->input->post('unit')),
			'total' => $this->input->post('total'),
			'detail' => $this->input->post('detail')
			
		);
		$insert = $this->medicines->insert_med($med_info);
		if($insert){
			if($this->input->post('time')){
				$time = array();
				$time_ex = explode(",",$this->input->post('time'));
				foreach($time_ex as $t){
					if($t != ""){
						$time[] = $t;
					}
				}
				for($i=0; $i<count($time); $i++){
					$history_info = array(
						'user_id' => $this->session->userdata('user_id'),
						'med_id' => $insert,
						'time_id' => $this->get_time($time[$i])
					);
					$notify_info = array(
						'user_id' => $this->session->userdata('user_id'),
						'med_id' => $insert,
						'time_id' => $this->get_time($time[$i]),
						'notify_time' => $this->calculate_notify_time($this->session->userdata('user_id'), $this->get_time($time[$i]), $insert)
					);
					$insert_not = $this->medicines->insert_notify($notify_info);
					$insert_his = $this->medicines->insert_user_med($history_info);
				}
			}
			else{
				$history_info = array(
					'user_id' => $this->session->userdata('user_id'),
					'med_id' => $insert,
					'time_id' => 5
				);
				$insert_his = $this->medicines->insert_user_med($history_info);
			}
		}
		else {
			echo 0;
		}
	}
	
	function get_routine($r){
		$routine = $this->medicines->get_routine_id($r);
		return $routine->routine_id;
	}
	function get_type($t){
		$type = $this->medicines->get_type_id($t);
		return $type->type_id;
	}
	function get_unit($u){
		$unit = $this->medicines->get_unit_id($u);
		if($unit) return $unit->unit_id;
		else return 0;
	}
	function get_time($tim){
		$time = $this->medicines->get_time_id($tim);
		return $time->time_id;
	}
	
	function calculate_notify_time($uid,$tid,$mid){
		$user_time = $this->medicines->get_routine_time($uid, $tid)->time;
		$med = $this->medicines->get_med($mid);
		$cal_time = $this->medicines->get_routine($med->routine_id)->cal_time;
		
		$cal = strtotime($user_time.$cal_time);
		$notify = date('H:i:s',$cal);
		
		return $notify;
	}
	
	function check_array(){
		$time = array();
		$time_ex = explode(",",$this->input->post('time'));
		foreach($time_ex as $t){
			if($t != ""){
				$time[] = $t;
			}
		}
		
		echo $this->get_time($time[1]);
	}
	
	
	function confirmed(){
		$med_id = array();
		$mid = explode(",",$this->input->post('med_id'));
		foreach($mid as $m){
			if($m != ""){
				$med_id[] = $m;
			}
		}
		$time_id = array();
		$tid = explode(",",$this->input->post('time_id'));
		foreach($tid as $t){
			if($t != ""){
				$time_id[] = $t;
			}
		}
		$uid = $this->session->userdata('user_id');
		for($i=0; $i<sizeof($med_id); $i++){
			$his_info = array(
				'user_id' => $uid,
				'med_id' => $med_id[$i],
				'time_id' => $time_id[$i],
				'date' => date("Y-m-d")
			);
			$add = $this->medicines->update_history($his_info,1);
			if($add){
				echo 1;
			}
			else echo 0;
		}
	}
	
	function unconfirmed(){
		$med_id = array();
		$mid = explode(",",$this->input->post('med_id'));
		foreach($mid as $m){
			if($m != ""){
				$med_id[] = $m;
			}
		}
		$time_id = array();
		$tid = explode(",",$this->input->post('time_id'));
		foreach($tid as $t){
			if($t != ""){
				$time_id[] = $t;
			}
		}
		$uid = $this->session->userdata('user_id');
		for($i=0; $i<sizeof($med_id); $i++){
			$his_info = array(
				'user_id' => $uid,
				'med_id' => $med_id[$i],
				'time_id' => $time_id[$i],
				'date' => date("Y-m-d")
			);
			$add = $this->medicines->update_history($his_info,2);
			if($add){
				echo 1;
			}
			else echo 0;
		}
		
	}
	
	
}